package com.frogs.ucenter.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.exception.BusinessException;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserIntegralBalance;
import com.frogs.ucenter.service.UserAccountInfoService;
import com.frogs.ucenter.service.UserIntegralBalanceService;

@Controller
@RequestMapping("/admin/userIntegralBalance")
public class UserIntegralBalanceController extends BaseController3 {

    private static final String EDIT_VIEW = "admin/userIntegralBalance/edit";

	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
    
    @Resource
    private UserIntegralBalanceService userIntegralBalanceService;

    @Resource
    private UserAccountInfoService userAccountInfoService;    
    
    @RequestMapping(EDIT)
    public String createUserIntegralBalance(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
        	model.put( "userAccountInfo",userAccountInfoService.getById( id ) );
            model.put( "model",userIntegralBalanceService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(UPDATE)
    public String updateUserIntegralBalance(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserIntegralBalance userIntegralBalanceNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        Integer oldBalance = reqInfo.getIntParameter( "oldBalance", 0);
        String updateReason = reqInfo.getParameter("updateReason","");
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserIntegralBalance userIntegralBalance = userIntegralBalanceService.getById( id ); 
                if(userIntegralBalance == null){
                	int balance = reqInfo.getIntParameter("integralUsable", 0);
                	userIntegralBalanceService.add(balance, updateReason, permissionHelper.getUser().getUserId(), id);
                }else{
                	 PropertyUtils.copyProperties(userIntegralBalance, userIntegralBalanceNew);
                     userIntegralBalanceService.update( userIntegralBalance,oldBalance ,updateReason, permissionHelper.getUser().getUserId()) ; 

                }
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (BusinessException e) {
                builder = DWZResponse.getFailBuilder("更新失败:" + e.getMessage());
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新失败:" + e.getMessage());
            } 
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }
}